#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source $DIR/utils.sh


NFSCLIENT=127.0.0.1
NFSSERVER=127.0.0.1
RUN_CLIENT="ssh -p 6636 root@$NFSCLIENT"
export PATH=/opt/pureflash:$PATH

LOOP_CNT=0
NUM_LEVELS=7
declare -A fio_IOPS
declare -A wr_BYTES
declare -A rd_BYTES

declare -A FIO_TYPE

function stat_pfs_io(){
	echo -e $(curl "http://localhost:49181/debug?op=disp_io" | jq '."perf_stat"')|tr -d '"' | sed 's/^ //g' | awk -F'[: ]' 'BEGIN{RD=0; WR=0} {RD=RD+$11; WR=WR+$9 } END{print  RD " " WR}' 
	
}

function stat_fs_iops(){
	grep iops $1 | awk -F[=,] '{sum+=$6}END{printf "%d",  sum;}'
}

function do_test(){
	info "Kill old pfs and ganesha, 8s"
	pkill ganesha.nfsd
	PFSID=$(pidof pfs)
	if [ "$PFSID" != "" ] ; then
		pkill -SIGINT pfs
		#wait $PFSID
	fi
	sleep 8

 
	/opt/pureflash/restart-pfs.sh

	echo -n "Waiting pfs ready"
	sleep 5
	while ! pfcli list_disk | grep OK ; do
		sleep 1
		echo -n "."
	done
	echo "Disk ready"
	for f in $(pfcli list_volume | tail -n +5 | awk -F\| '{print $3}' ); do pfcli delete_volume -v $f; done

	 
	assert mkfs.vn /vivenas_a

	info "Restart nfsd"
	rm -f vn.log vn2.log
	ganesha.nfsd -F  -f ./ganesha-vivenas.conf  -p /var/run/ganesha.pid -L vn2.log &> vn.log &
	NFSD_PID=$!
	sleep 2
	assert_proc $NFSD_PID
	echo -n "Wait ganesha.nfsd ready ..."
	while ! grep "NFS SERVER INITIALIZED" vn2.log; do sleep 1; echo -n "."; done
	echo "Restarted"



	$RUN_CLIENT mkdir test
	assert $RUN_CLIENT mount $NFSSERVER:/some test

	FIO_BS=4K
	FIO_TYPE[$LOOP_CNT]="RW_${FIO_BS}"
	curl "http://localhost:49181/debug?op=disp_io_reset"
	FIOLOG="fio_wr_$LOOP_CNT.log"
	assert $RUN_CLIENT  fio -filename=./test/file_fio.dat -size=4G -direct=1 -iodepth=2 -thread -rw=randwrite  -ioengine=libaio -bs=$FIO_BS -numjobs=2 -runtime=30 -group_reporting -name=randw0 > $FIOLOG

	fio_IOPS[$LOOP_CNT]=$(stat_fs_iops $FIOLOG)

	info "Wait 30s for compaction"
	sleep 60
	read rd_BYTES[$LOOP_CNT] wr_BYTES[$LOOP_CNT] <<< $(stat_pfs_io )
	curl "http://localhost:49181/debug?op=disp_io_reset"

	LOOP_CNT=$((LOOP_CNT+1))
	for FIO_BS in 4K 8K 16K 32K 64K 4K ; do
		FIO_TYPE[$LOOP_CNT]="RR_${FIO_BS}"
		curl "http://localhost:49181/debug?op=disp_io_reset"
		FIOLOG="fio_rd_$LOOP_CNT.log"
		assert $RUN_CLIENT  fio -filename=./test/file_fio.dat -size=4G -direct=1 -iodepth=8 -thread -rw=randread  -ioengine=libaio -bs=$FIO_BS -numjobs=4 -runtime=30 -group_reporting -name=randw0 > $FIOLOG
		fio_IOPS[$LOOP_CNT]=$(stat_fs_iops $FIOLOG)
		#curl "http://localhost:49181/debug?op=perf" | jq 
		read rd_BYTES[$LOOP_CNT] wr_BYTES[$LOOP_CNT] <<< $(stat_pfs_io )
		LOOP_CNT=$((LOOP_CNT+1))
	done 
	#curl "http://localhost:49181/debug?op=get_obj_count"
	 
	 
	assert $RUN_CLIENT   umount test
	kill  $NFSD_PID
	assert wait $NFSD_PID
	sleep 3
	info "Test on loop $LOOP_CNT complete"
}

for lv in 7 4 2; do 
	NUM_LEVELS=$lv
	info "=============`date` Start test with num_levels=$NUM_LEVELS============"
	echo -e "[rocksdb]\nnum_levels=$NUM_LEVELS" > /etc/pureflash/vivenas.conf

	do_test 
done


echo "Op      fio_iops    pfs_wr_GB(Write)    pfs_rd_GB(Write) " 
for ((i=0;i<$LOOP_CNT;i++)); do
	printf "%-8s%-12d%-20d%-20d\n" ${FIO_TYPE[$i]} ${fio_IOPS[$i]}    $((${wr_BYTES[$i]}>>30)) $((${rd_BYTES[$i]}>>30))
done	